﻿using Cysharp.Threading.Tasks;
using UnityEngine;

/// <summary>
/// 下跳（player专用）
/// </summary>
public class FSMStateJumpAcross : FSMStateBase
{
    protected override string getName => "jumpAcross";
    protected override string InitClipName() => "duration";

    public override bool isLoop => false;

    public override int priority { get; } = UnitDefine.STATE_PRIORITY_NON_INTERRUPTIBLE;

    private float riseHeight => UnitDefine.JUMP_ACROSS_RISE_HEIGHT;

    protected override void OnEnter()
    {
        JumpAcross();
    }

    private async void JumpAcross()
    {
        fsm.entity.physics.groundChecker.colliBox.isTrigger = true;
        var initialSpeedY = fsm.entity.physics.FFMEndSpeed(riseHeight);
        fsm.entity.physics.velocity = new Vector2(0, initialSpeedY);
        await UniTask.WaitUntil(() => 
            fsm.entity.physics.velocity.y < -initialSpeedY &&
            !fsm.parameters.GetParam<bool>("isGround"));
        fsm.entity.physics.groundChecker.colliBox.isTrigger = false;
        isEnd = true;
    }
}